package  com.cube
{
	import com.adobe.ac.mxeffects.CubeRotate;
	import com.adobe.ac.mxeffects.DistortionConstants; 
	
	import mx.core.UIComponent;
	import mx.events.EffectEvent; 
	
	public class EDICubeRotate 
	{
		public var cr:com.adobe.ac.mxeffects.CubeRotate;
		private var list:Array;   
		private var _index:int=0; 
		public var effectEndCall:Function;
		public var effectStartCall:Function;
		public var beginPlanCall:Function;
		public var endPlanCall:Function;
		
		
		private var activeComp:UIComponent=null;
		private var oldComp:UIComponent=null;
		private var next:Boolean=true;
		public var cycle:Boolean=false;
		 
		private var compPlan:UIComponent;
		public function EDICubeRotate(compPlan:UIComponent)
		{
			cr=new com.adobe.ac.mxeffects.CubeRotate();
			this.compPlan=compPlan;
			list=new Array();
			cr.duration = 1000;
			cr.horizontalLightingLocation = DistortionConstants.LEFT;
			cr.addEventListener(EffectEvent.EFFECT_END,effectEndHandler);
			cr.addEventListener(EffectEvent.EFFECT_START,effectStartHandler);
		}
		
		public function get index():int{
			return _index;
		}
		
		private function effectEndHandler(event:EffectEvent):void
		{
			trace(oldComp.parent);
			compPlan.removeChild(oldComp);
			next=true;
			if(effectEndCall!=null){effectEndCall.call(this,event);}
		}
		
		private function effectStartHandler(event:EffectEvent):void
		{
			next=false;
			if(effectStartCall!=null){effectStartCall.call(this,event);}
		}
		
		
		public function add(value:UIComponent):void
		{
			list.push(value);
		}
		
		public  function moveNext():void
		{
			if(next)
			{ 
				if(cycle){
					if(_index==list.length-1){
						_index=-1;
					}
				}
				if(_index<list.length-1){
					oldComp=activeComp==null?list[_index]:activeComp;
					activeComp=list[++_index];
				}
				else
				{
					return;
				}
				compPlan.addChild(activeComp);
				cr.target=oldComp;
				cr.siblings=[activeComp];
				cr.direction = DistortionConstants.LEFT ;
				cr.play();
			
			}
		}
		
		public function movePrevious():void
		{
			if(next)
			{ 
				if(cycle)
				{
					if(_index<=0){
						_index=list.length;
					}
				}
				if(_index>0)
				{
					oldComp=oldComp=activeComp==null?list[0]:activeComp;
					activeComp=list[--_index];
				}
				else
				{
					return 
				}
				compPlan.addChild(activeComp);
				cr.target=oldComp;
				cr.siblings=[activeComp];
				cr.direction = DistortionConstants.RIGHT;
				cr.play();
			}			
		}
		
		public function goto(toIndex:int,fromIndex:int=0):void
		{
			if(next && index>-1 && index<list.length)
			{

				oldComp=fromIndex!=0?list[fromIndex]:(activeComp==null?list[0]:activeComp);
				activeComp=list[toIndex];
				if(oldComp!=activeComp)
				{
					compPlan.addChild(activeComp);
					cr.target=oldComp;
					cr.siblings=[activeComp];
					cr.direction = DistortionConstants.LEFT ;
					cr.play();
				}
				this._index=toIndex;
			}
		}
	}
}